<?php session_start();   ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php 
if(isset($_GET['deconnexion'])){
//****************************     déconnexion    *****************************************
//                            

session_unset();
session_destroy();
header ('location: ../index.php '); 
exit();

}
//***************************************************************************************
if (isset($_POST['Authentifier'])&& $_POST['Authentifier'] == 'Authentifier'){ 
$message ="je suis la bas";
     if ((isset($_POST['login']) && !empty($_POST['login'])) &&
	     (isset($_POST['mot_passe']) && !empty($_POST['mot_passe']))&&
		 (isset($_POST['statut']))&& !empty($_POST['statut'])){

//******************************************************************** connexion base de données
		 try
{
	$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
	 $bdd = new PDO('mysql:host=localhost;dbname=gpfe.db', 'root', '', $pdo_options);
}
catch(Exception $e)
{
        die('Erreur : '.$e->getMessage());
}
//***********************************************************************
$statut =$_POST['statut'];
$login = $_POST['login'];
if (($statut == "CDD")||($statut == "encadreur")||($statut =="mcv")){
//**************************************************************************
//                  interfaces chef du département/ MCV / encadreur 
//*************************************************************************	
	$retour =$bdd->prepare("SELECT * FROM enseignant WHERE login='" .$_POST['login']."'AND mot_passe='".$_POST['mot_passe']."'And statut='".$_POST['statut']."'");
	$retour->execute();
	$donnees= $retour->fetch();
	$nbr=$retour->rowCount();
	
	 if ($nbr == 1){
	    $_SESSION['login'] = $login;
		$_SESSION['statut']=$statut;
		$_SESSION['id']= $donnees['id_ens'];
	    header('Location: profil.php');
	    }elseif($nbr == 0){
	$_SESSION['erreur']= 'Compte non reconnu.';	 
	   header('Location: ../index.php'); 
	 }
	 elseif($nbr > 1){
		 $_SESSION['erreur']= 'erreur';	
		 header('Location: ../index.php'); 
	 }
}
elseif($statut == "etudiant"){
//**************************************************************************
//                  interfaces étudiant
//*************************************************************************	
	$retour =$bdd->prepare("SELECT * FROM etudiant WHERE login='" .$_POST['login']."'AND mot_passe='".$_POST['mot_passe']."'");
	$retour->execute();
	$nbr=$retour->rowCount();
	$donnees= $retour->fetch();
	
	if ($nbr == 1){
	   
	// if ($_POST['mot_passe'] == $donnees['mot_passe']){
	 $_SESSION['login'] = $login;
	 $_SESSION['statut']=$statut;
	 $_SESSION['id']= $donnees['id_ens'];
	 header('Location: profil.php');
	 }elseif($nbr == 0){
	$_SESSION['erreur']= 'Compte non reconnu.';	 
	   header('Location: ../index.php'); 
	 }

}		 
}
else{
	echo "l'un des champs est vide !!!!";
	}
}

echo $message;
?>

</body>
</html>